package client_agent

import (
	"github.com/pkg/errors"
	"github.com/zeast/logs"
	"lark/conf"
)

const (
	newFlag uint32 = iota
	prepareFlag
	runningFlag
	closedFlag
)

var logger *logs.Logger

func SetLoggerLevel(lv string) {
	logger.SetLogLevelStr(lv)
}

func LoggerLevel() string {
	return logger.LogLevelStr()
}

func initLogger() error {
	w, err := logs.NewFileWriter(logs.FileConfig{
		Name: conf.ClientAgentLogFileName,
		Perm: conf.ClientAgentLogPerm,
		Rotate: &logs.Rotate{
			MaxSize: conf.ClientAgentLogMaxSize,
			MaxDays: conf.ClientAgentLogMaxDays,
			Perm:    conf.ClientAgentLogRotatePerm,
			Daily:   conf.ClientAgentLogDaily,
		},
	})

	if err != nil {
		return errors.WithStack(err)
	}

	logger = logs.NewLogger(w)
	logger.LogFuncCall(true)
	logger.LogFuncCallDepth(4)
	logger.SetLogLevelStr(conf.ClientAgentLogLevel)
	logger.SetTimeLayout("2006-01-02 15:04:05.000")

	return nil
}
